package JavaSE.ClassFilePrefix;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *  测试 Java编译后生成的class字节码文件前缀4个字节是否相同
 *
 *      结果：前四个字节相同
 */
public class Test {
    public static void main(String[] args) throws IOException {
        FileInputStream fis1 = new FileInputStream("src\\JavaSE.ClassFilePrefix\\ClassFiles\\BufferedSingle.class");
        FileInputStream fis2 = new FileInputStream("src\\JavaSE.ClassFilePrefix\\ClassFiles\\TreeNodes.class");
        FileInputStream fis3 = new FileInputStream("src\\JavaSE.ClassFilePrefix\\ClassFiles\\JavaSE.SETest.class");
        FileOutputStream fos1 = new FileOutputStream("src\\JavaSE.ClassFilePrefix\\ClassFiles\\BufferedSingle.txt");
        FileOutputStream fos2 = new FileOutputStream("src\\JavaSE.ClassFilePrefix\\ClassFiles\\TreeNodes.txt");
        FileOutputStream fos3 = new FileOutputStream("src\\JavaSE.ClassFilePrefix\\ClassFiles\\JavaSE.SETest.txt");
        int readCount;
        byte[] bytes = new byte[1024];
        while((readCount = fis1.read(bytes)) != -1) {
            fos1.write(bytes,0,readCount);
        }
        while((readCount = fis2.read(bytes)) != -1) {
            fos2.write(bytes,0,readCount);
        }
        while((readCount = fis3.read(bytes)) != -1) {
            fos3.write(bytes,0,readCount);
        }
        fis1.close();
        fis2.close();
        fis3.close();
        fos1.close();
        fos2.close();
        fos3.close();

        byte[] bytes1 = new byte[12];
        FileInputStream fis4 = new FileInputStream("src\\JavaSE.ClassFilePrefix\\ClassFiles\\BufferedSingle.txt");
        printThePrefix(bytes1, fis4);
        fis4 = new FileInputStream("src\\JavaSE.ClassFilePrefix\\ClassFiles\\TreeNodes.txt");
        printThePrefix(bytes1, fis4);
        fis4 = new FileInputStream("src\\JavaSE.ClassFilePrefix\\ClassFiles\\JavaSE.SETest.txt");
        printThePrefix(bytes1, fis4);

        fis4.close();

    }

    private static void printThePrefix(byte[] bytes1, FileInputStream fis4) throws IOException {
        fis4.read(bytes1);
        for (int i = 0; i < bytes1.length; i++) {
            System.out.print(bytes1[i]);
        }
        System.out.println();
    }
}
